#ifndef __CHELPER_UTILS_TIMER_H__
#define __CHELPER_UTILS_TIMER_H__

#include <unistd.h>
#include <chrono>

namespace chelper {
namespace utils {

using TimeT = std::chrono::milliseconds;
using ClockT = std::chrono::steady_clock;
using time_t = std::chrono::steady_clock::time_point;

class Timer {
  public:
    Timer() = default;

    ///
    /// @brief start timing
    ///
    void start() {
        _start = ClockT::now();
        _end = {};
    }

    ///
    /// @brief end timing
    ///
    void end() { _end = ClockT::now(); }

    ///
    /// @brief duration time by milliseconds
    ///
    /// @return  long
    ///
    long durationMilli() {
        TimeT d = std::chrono::duration_cast<TimeT>(_end - _start);
        return d.count();
    }

    ///
    /// @brief duration time by seconds
    ///
    /// @return  double
    ///
    double duration() {
        TimeT d = std::chrono::duration_cast<TimeT>(_end - _start);
        return (double)d.count() / 1000;
    }

  private:
    time_t _start;
    time_t _end;
};

}  // namespace utils
}  // namespace chelper

#endif  // __CHELPER_UTILS_TIMER_H__
